using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Q.Framework.Data;
using System.Data.Objects;
using Q.Framework.WebControls;
using Q.Framework.Extension;

namespace Q.Framework.WebCode
{
    abstract public class BasePageSearch<C> : BasePageSearch
    {
        private C controller;

        public C Controller
        {
            get
            {
                if (controller == null)
                    controller = ControllerBase.Create<C>();

                return controller;
            }
        }
    }

    abstract public class BasePageSearch : PageBase
    {
        public Int32? ItemsCount
        {
            get
            {
                return ((SearchMasterPageBase)Master).ItemsCount;
            }
            set
            {
                ((SearchMasterPageBase)Master).ItemsCount = value;
            }
        }

        public Boolean IsSearched { get; set; }
        private SearchMasterPageBase masterSearch;

        public SearchMasterPageBase MasterSearch
        {
            get
            {
                if (masterSearch == null)
                    masterSearch = (SearchMasterPageBase)Master;
                return masterSearch;
            }
        }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            if (!DesignMode)
            {
                ((SearchMasterPageBase)Master).RedirectPageInsert += RedirectPageInsert;
                
                ((SearchMasterPageBase)Master).Search += Search;
                ((SearchMasterPageBase)Master).Search += AfterSearch;
                ((SearchMasterPageBase)Master).New += New;
                ((SearchMasterPageBase)Master).Report += Report;
            }

            base.OnInit(e);
        }

        private void AfterSearch(object sender, EventArgs e)
        {
            ((SearchMasterPageBase)Master).IsSearched = true;
        }

        abstract protected void Search(object sender, EventArgs e);

        virtual protected void RedirectPageInsert(object sender, EventArgs e)
        {
            String page = System.Configuration.ConfigurationManager.AppSettings["DefaultCrudPageName"] ?? "PageCrud.aspx";
            Redirect(page);

        }

        virtual protected void New(object sender, EventArgs e) { }

        virtual protected void Report(object sender, EventArgs e) { }
    }
}